#include "filesystemmodel.h"
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <QDebug>
#include <QDesktopServices>
#include "common.h"
#include <QDir>

FileSystemModel::FileSystemModel(QObject *parent) :
    QFileSystemModel(parent)
{

    QHash<int, QByteArray> roles;
    roles[FileNameRole] = "fileName";
    roles[FilePathRole] = "filePath";
    roles[SizeRole] = "size";
    roles[LastModifiedRole] = "lastModified";
    setRoleNames(roles);


    //    setSorting(QDir::DirsFirst);



}

bool FileSystemModel::isDir(const QModelIndex &index) const
{
    return QFileSystemModel::isDir(index);
}

QString FileSystemModel::filePath(const QModelIndex &index) const
{
    return QFileSystemModel::filePath(index);
}

QVariant FileSystemModel::data(const QModelIndex &index, int role) const
{

    if ( role == SizeRole)
    {
        QFileInfo info(data(index,FilePathRole).toString());
        return Common::sizeToString(info.size());


    }
    if ( role == LastModifiedRole)
    {
        QFileInfo info(data(index,FilePathRole).toString());
        return info.lastModified();


    }

    return QFileSystemModel::data(index,role);
}


void FileSystemModel::setDir(const QString &path)
{
    setRootPath(path);
    mPathIndex = index(path);
    emit pathIndexChanged();
}



void FileSystemModel::actionRename(const QModelIndex & index, const QString &name)
{



}

void FileSystemModel::actionRemove(const QModelIndex & index)
{
    remove(index);
}

void FileSystemModel::actionMkdir(const QModelIndex & index, const QString &name)
{

    qDebug()<<"mkdir "<<name;

    if ( index.parent().isValid())
        mkdir(index.parent(), name);
}

